package com.epfresh.supplier.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.epfresh.api.constant.Constant;
import com.epfresh.supplier.R;
import com.epfresh.supplier.activity.H5OrderDetailActivity;
import com.epfresh.supplier.entity.BookDayDetail;
import com.epfresh.supplier.entity.BookGoodsMonth;

import java.util.List;

/**
 * Created by wzh on 2016/5/6.
 */
public class BookGoodsMonthExpandListAdapter extends BaseExpandableListAdapter implements View.OnClickListener {

    public interface OnCheckChangeListener {
        public void onCheckChange(boolean isCheck);
    }
    private OnCheckChangeListener onCheckChangeListener;
    public ExpandableListView lv;

    public Context context;
    public List<BookGoodsMonth.SupplyOrderIncomeDayListBean> groupList;
    private AdapterView.OnItemClickListener onItemClickListener;

    private OnBottomClickListener onBottomClickListener;
    private OnNotifyDataSetChangedListener onNotifyDataSetChangedListener;


    public BookGoodsMonthExpandListAdapter(Context context, ExpandableListView lv, List<BookGoodsMonth.SupplyOrderIncomeDayListBean> groupList) {
        this.context = context;
        this.groupList = groupList;
        this.lv = lv;
    }

    public OnCheckChangeListener getOnCheckChangeListener() {
        return onCheckChangeListener;
    }

    public void setOnCheckChangeListener(OnCheckChangeListener onCheckChangeListener) {
        this.onCheckChangeListener = onCheckChangeListener;
    }

    @Override
    public void notifyDataSetChanged() {
        if (onNotifyDataSetChangedListener != null) {
            onNotifyDataSetChangedListener.OnNotifyDataSetChangedListener(groupList.size());
        }
        expendList();
        super.notifyDataSetChanged();
    }


    public OnNotifyDataSetChangedListener getOnNotifyDataSetChangedListener() {
        return onNotifyDataSetChangedListener;
    }

    public void setOnNotifyDataSetChangedListener(OnNotifyDataSetChangedListener onNotifyDataSetChangedListener) {
        this.onNotifyDataSetChangedListener = onNotifyDataSetChangedListener;
    }

    public OnBottomClickListener getOnBottomClickListener() {
        return onBottomClickListener;
    }

    public void setOnBottomClickListener(OnBottomClickListener onBottomClickListener) {
        this.onBottomClickListener = onBottomClickListener;
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        List<BookGoodsMonth.SupplyOrderIncomeDayListBean.DetailListBean> configs = groupList.get(groupPosition).getDetailList();

/*        List<StatisticsListEntity.ResultsBean.ResultsChildBean> configs = new ArrayList<>();
        for(int i=0;i<groupList.get(groupPosition).getResults().size();i++){
            configs.addAll(groupList.get(groupPosition).getResults().get(i).getResults());
        }*/
        return configs == null ? 0 : configs.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {

        return groupList.get(groupPosition).getDetailList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    /**
     * 组和子元素是否持有稳定的ID,也就是底层数据的改变不会影响到它们。
     *
     * @return
     * @see android.widget.ExpandableListAdapter#hasStableIds()
     */
    @Override
    public boolean hasStableIds() {
        return true;
    }

    /**
     * 获取显示指定组的视图对象
     *
     * @param groupPosition 组位置
     * @param isExpanded    该组是展开状态还是伸缩状态
     * @param convertView   重用已有的视图对象
     * @param parent        返回的视图对象始终依附于的视图组
     * @return
     * @see android.widget.ExpandableListAdapter#getGroupView(int, boolean, View,
     * ViewGroup)
     */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_string_book_parent, null, false);
            vh.tv_name = (TextView)convertView.findViewById(R.id.tv_name);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        vh.tv_name.setText(groupList.get(groupPosition).getName());
        //vh.tv_time.setOnClickListener(null);
        convertView.setTag(vh);
        return convertView;
    }

    /**
     * 获取一个视图对象，显示指定组中的指定子元素数据。
     *
     * @param groupPosition 组位置
     * @param childPosition 子元素位置
     * @param isLastChild   子元素是否处于组中的最后一个
     * @param convertView   重用已有的视图(View)对象
     * @param parent        返回的视图(View)对象始终依附于的视图组
     * @return
     * @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean, View,
     * ViewGroup)
     */
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder vh = null;
        if (convertView == null) {
            vh = new ChildViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_string_book_child, null, false);
            vh.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            vh.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
            vh.tv_number = (TextView) convertView.findViewById(R.id.tv_number);
            vh.ll_item = convertView.findViewById(R.id.ll_item);
            convertView.setTag(vh);
        } else {
            vh = (ChildViewHolder) convertView.getTag();
        }

        BookGoodsMonth.SupplyOrderIncomeDayListBean logisticsOrder = groupList.get(groupPosition);
        List<BookGoodsMonth.SupplyOrderIncomeDayListBean.DetailListBean> orders = logisticsOrder.getDetailList();
        vh.tv_name.setText(orders.get(childPosition).getDescription());
        vh.tv_money.setText(orders.get(childPosition).getPriceTitle());
        vh.tv_number.setText(orders.get(childPosition).getMerName());

        vh.ll_item.setTag(R.id.item_key_position, groupPosition * 100 + childPosition);
        vh.ll_item.setOnClickListener(this);
        return convertView;
    }

    @Override
    public void onClick(View v) {
        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer) {
            int position = (int) tag;
            switch (v.getId()) {
/*                case R.id.iv_phone:
                    Intent call = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + groupList.get(position / 100).getResults().get(position%100).getDistributionPhone()));//调出来拨打页面；
                    context.startActivity(call);
                    break;*/
                case R.id.ll_item:
                    Log.i("点击了","__id"+position/100+"___"+position%100);
                    Intent detail = new Intent(context,H5OrderDetailActivity.class);
                    detail.putExtra("orderId", groupList.get(position/100).getDetailList().get(position%100).getOrderId());
                    detail.putExtra(Constant.KEY_WEB_URL, "http://m.epfresh.com/help/order_supply_new.html?orderId="+groupList.get(position/100).getDetailList().get(position%100).getOrderId());
                    detail.putExtra(Constant.KEY_WEB_TITLE, "订单详情");
                    context.startActivity(detail);
                    break;
            }
        }
    }


    /**
     * 是否选中指定位置上的子元素。
     *
     * @param groupPosition
     * @param childPosition
     * @return
     * @see android.widget.ExpandableListAdapter#isChildSelectable(int, int)
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }


    protected void expendList() {

        if (lv != null)
            for (int i = 0; i < groupList.size(); i++) {
                lv.expandGroup(i);
            }
    }

    public class ViewHolder {
        TextView tv_name;
    }

    public class ChildViewHolder {
        TextView tv_name;
        TextView tv_money;
        TextView tv_number;
        View ll_item;
    }

    public interface OnBottomClickListener {

        public void onBottomClickListener(int position, int type);
    }

    public interface OnNotifyDataSetChangedListener {

        public void OnNotifyDataSetChangedListener(int size);
    }
}
